{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using packages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we will use a local Weka jar and make use of the `gridSearch` package (which needs to be installed)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Classpath\n",
    "Instead of using a maven dependency, we use a specific Weka jar located in my home directory.\n",
    "\n",
    "You will need to adjust this to make it work on your machine.\n",
    "\n",
    "*Windows users:* use forward slashes instead of backslashes, eg: `C:/somewhere/weka.jar`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "%jars /home/fracpete/programs/weka/weka-3-9-3/weka.jar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports\n",
    "Import the required classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import weka.classifiers.Classifier;\n",
    "import weka.core.Utils;\n",
    "import weka.core.WekaPackageManager;\n",
    "import weka.core.packageManagement.Package;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Package support\n",
    "Now load all the installed packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "WekaPackageManager.loadPackages(false);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now check whether the `gridSearch` package is installed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "boolean gridSearchInstalled = false;\n",
    "for (Package p: WekaPackageManager.getInstalledPackages()) {\n",
    "  if (p.getName().equals(\"gridSearch\")) {\n",
    "    gridSearchInstalled = true;\n",
    "    break;\n",
    "  }\n",
    "}\n",
    "if (!gridSearchInstalled)\n",
    "  throw new IllegalStateException(\"Please install the gridSearch package first!\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Instantiate classifier\n",
    "Since the `GridSearch` meta-classifier is dynamically loaded from a package, we cannot import its class directly. \n",
    "\n",
    "Instead, we use the `forName` method of the `weka.core.Utils` class to instantiate an instance of it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weka.classifiers.meta.GridSearch -E CC -y-property kernel.gamma -y-min -3.0 -y-max 3.0 -y-step 1.0 -y-base 10.0 -y-expression pow(BASE,I) -x-property C -x-min -3.0 -x-max 3.0 -x-step 1.0 -x-base 10.0 -x-expression pow(BASE,I) -sample-size 100.0 -traversal ROW-WISE -log-file /home/fracpete/programs/java-notebooks -num-slots 1 -S 1 -W weka.classifiers.functions.GaussianProcesses -- -L 1.0 -N 0 -K \"weka.classifiers.functions.supportVector.PolyKernel -E 1.0 -C 250007\" -S 1\n"
     ]
    }
   ],
   "source": [
    "Classifier gridSearch = (Classifier) Utils.forName(Classifier.class, \"weka.classifiers.meta.GridSearch\", new String[0]);\n",
    "System.out.println(Utils.toCommandLine(gridSearch));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".java",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "10.0.2+13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
